package com.mq.listeners;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.stereotype.Component;

import javax.naming.Name;
import java.io.IOException;
import java.time.LocalDateTime;

/**
 * ClassName: ConsumerListener
 * Package: com.mq.listeners
 * Description:
 *
 * @Author: @weixueshi
 * @Create: 2024/8/8 - 9:35
 * @Version: v1.0
 */
@Component
public class ConsumerListener {

//    @RabbitListener(queues = "simple.queue")
//    public void simpleConsumer(String msg){
//        System.out.println("简单模式下消费者接收到消息:"+msg);
//    }
//
//    @RabbitListener(queues = "work.queue")
//    public void workConsumer1(String msg) throws InterruptedException {
//        System.out.println("消费者1接收到消息:"+msg);
//        //睡眠20毫秒
//        Thread.sleep(20);
//    }
//
//    @RabbitListener(queues = "work.queue")
//    public void workConsumer2(String msg) throws InterruptedException {
//        System.err.println("消费者2接收到消息........."+msg);
//        //睡眠200毫秒
//        Thread.sleep(200);
//    }
//
//    @RabbitListener(queues = "fanout.queue1")
//    public void fanoutConsumer1(String msg) throws InterruptedException {
//        System.out.println("消费者1接收到消息:"+msg);
//    }
//
//    @RabbitListener(queues = "fanout.queue2")
//    public void fanoutConsumer2(String msg) throws InterruptedException {
//        System.err.println("消费者2接收到消息........."+msg);
//    }
//
//    @RabbitListener(queues = "direct.queue1")
//    public void directConsumer1(String msg) {
//        System.out.println("direct.queue1接收到消息:"+msg);
//    }
//
//    @RabbitListener(queues = "direct.queue2")
//    public void directConsumer2(String msg) {
//        System.err.println("direct.queue2接收到消息........."+msg);
//    }
//
//    @RabbitListener(queues = "topic.queue1")
//    public void topicConsumer1(String msg) {
//        System.out.println("topic.queue1接收到消息:"+msg);
//    }
//
//    @RabbitListener(queues = "topic.queue2")
//    public void topicConsumer2(String msg) {
//        System.err.println("topic.queue2接收到消息........."+msg);
//    }

//    @RabbitListener(bindings = @QueueBinding(
//            value = @Queue(name = "direct.queue1", durable = "true"),
//            exchange = @Exchange(name = "direct.exchange", type = ExchangeTypes.DIRECT),
//            key = {"red", "blue"}
//    ))
//    public void directConsumer1(String msg) {
//        System.out.println("direct.queue1接收到消息:"+msg);
//    }
//
//    @RabbitListener(bindings = @QueueBinding(
//            value = @Queue(name = "direct.queue2", durable = "true"),
//            exchange = @Exchange(name = "direct.exchange", type = ExchangeTypes.DIRECT),
//            key = {"red", "yellow"}
//    ))
//    public void directConsumer2(String msg) {
//        System.out.println("direct.queue2接收到消息:"+msg);
//    }
//
//    @RabbitListener(queues = "object.queue")
//    public void objectConsumer(Map<String, Object> msg) {
//        System.out.println("object.queue接收到消息:"+msg);
//    }

//    @RabbitListener(queuesToDeclare = @Queue(
//            name = "lazy.queue",
//            durable = "true",
//            arguments = @Argument(name = "x-queue-mode", value = "lazy")
//    ))
//    public void lazyConsumer(String msg) {
//        System.out.println("lazy.queue接收到消息:"+msg);
//    }

    /**
     * 监听死信队列的消费者
     */
//    @RabbitListener(queues = "dead.queue")
//    public void deadConsumer(String msg) {
//        System.out.println("死信队列的消费者接收到死信消息:"+msg);
//    }


    /**
     * 订单死信队列消费者
     * @param orderId
     */
//    @RabbitListener(bindings = @QueueBinding(
//            value = @Queue(name = "dead.order.queue", durable = "true"),
//            exchange = @Exchange(name = "dead.order.exchange"),
//            key = "order-routing"
//    ))
//    public void orderDeadConsumer(String orderId) {
//        System.out.println(orderId+"：订单已过期,准备取消订单");
//    }

    /**
     * 监听死信队列的消费者
     * @param msg
     */
//    @RabbitListener(bindings = @QueueBinding(
//            value = @Queue(name = "dead.queue", durable = "true"),
//            exchange = @Exchange(name = "dead.exchange"),
//            key = "dead-routing"
//    ))
//    public void deadConsumer(String msg, Message message, Channel channel) throws IOException {
//        System.err.println("接收到消息:"+msg + "。接收消息时间：" + LocalDateTime.now());
//    }

    /**
     * 监听普通队列的消费者
     * @param msg
     */
//    @RabbitListener(bindings = @QueueBinding(
//            value = @Queue(
//                    name = "normal.queue",
//                    durable = "true",
//                    arguments = {
//                            @Argument(name = "x-dead-letter-exchange", value = "dead.exchange"),
//                            @Argument(name = "x-dead-letter-routing-key", value = "dead-routing"),
//                            @Argument(name = "x-max-length", value = "5",type = "java.lang.Integer")
//                    }
//            ),
//            exchange = @Exchange(name = "normal.exchange"),
//            key = "normal-routing"
//    ))
//    public void normalConsumer(String msg, Message message, Channel channel) throws IOException {
//        System.out.println("我是普通队列消费者，接收到消息:"+msg);
//    }

    /**
     * 监听延迟队列的消费者
     * @param msg
     */
    @RabbitListener(queues = "delayed.queue")
    public void delayedConsumer(String msg) {
        System.out.println("接收到消息:"+msg + "。接收消息时间：" + LocalDateTime.now());
    }
}
